//
// Created by ta&to on 2022/10/28.
//
#include "http_net_config.h"
void init_req_attr(struct request_head_attr_s* attr) {
    attr->Method = NULL;
    attr->path = NULL;
    attr->Cache_control = NULL;
    attr->User_Agent = NULL;
    attr->Accept = NULL;
    attr->Accept_Language = NULL;
    attr->Accept_encoding = NULL;
    attr->Content_Length = 0;
    attr->Host = NULL;
    attr->Connection = NULL;
    attr->Content_Type = NULL;
    attr->Origin = NULL;
    attr->Referer = NULL;
}
dts _req_attr_do(struct request_head_attr_s *attr, dts attr_str) {
    dts attr_key = dtsdup(attr_str);
    dts attr_val = dtsdup(attr_str);
    int idex = indexof(attr_key, ":");
    if(idex > -1) {
        dtsrange(attr_key, 0, idex - 1);
        dtsrange(attr_val, idex + 1, dtslen(attr_val));
        attr_key = dtstrim(attr_key, " ");
        attr_val = dtstrim(attr_val, " ");
        if (!dtscasecmpstr(attr_key, "cache-control")) {
            dts_auto_make(&attr->Cache_control, attr_val);
        }else if (!dtscasecmpstr(attr_key, "User-Agent")) {
            dts_auto_make(&attr->User_Agent, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Accept")) {
            dts_auto_make(&attr->Accept, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Accept-encoding")) {
            dts_auto_make(&attr->Accept_encoding, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Accept-Language")) {
            dts_auto_make(&attr->Accept_Language, attr_val);
        }  else if (!dtscasecmpstr(attr_key, "Host")) {
            dts_auto_make(&attr->Host, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Connection")) {
            dts_auto_make(&attr->Connection, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Content-Length")) {
            attr->Content_Length = atoi(attr_val);
            dtsfree(attr_val);
        } else if (!dtscasecmpstr(attr_key, "Content-Type")) {
            dts_auto_make(&attr->Content_Type, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Origin")) {
            dts_auto_make(&attr->Origin, attr_val);
        } else if (!dtscasecmpstr(attr_key, "Referer")) {
            dts_auto_make(&attr->Referer, attr_val);
        } else {
            dtsfree(attr_val);
        }
        dtsfree(attr_key);
    } else {
        dtsfree(attr_key);
        dtsfree(attr_val);
    }
}
dts _req_trim_head(dts head){
    dts head_ = dtsdup(head);
    int hidex = indexof(head, HEAD_TAIL);
    dtsrange(head_, 0, hidex + strlen(HEAD_TAIL) - 1);
    return head_;
}

void _req_analysis_head(req_header_t *header) {
    int idex;
    dts head_ = _req_trim_head(header->head);
    char method[12] = { 0 };
    char path[PATH_MAX] = { 0 };
    sscanf(head_, "%[^ ] %[^ ]", method, path);
    header->attr->Method = dtsnew(method);
    header->attr->path = dtsnew(path);
    dts attr__ = dtsdup(head_);
    while((idex = indexof(attr__, CRLF)) > -1) {
        dtsrange(attr__, 0, idex - 1);
        dtsrange(head_, idex + sizeof CRLF  - 1, dtslen(head_));
        _req_attr_do(header->attr, attr__);
        dtsfree(attr__);
        attr__ = dtsdup(head_);
    }
    dtsfree(attr__);
    dtsfree(head_);
}

void _req_analysis_body(req_header_t *header) {
    if(header->attr->Content_Length > 0){
        if(!header->body)
            header->body = dtsempty();
        header->req_stream.need_read_len = header->attr->Content_Length;
        header->body = dtscatlen(header->body, header->buf, header->buf_len);
        header->req_stream.has_readed += header->buf_len;
        header->req_stream.need_read_len = header->req_stream.need_read_len - header->req_stream.has_readed;
        header->req_stream.read_end = (header->req_stream.need_read_len == 0) ? 1 : 0;
        header->req_stream.reading = (header->req_stream.need_read_len > 0) ? 0 : 1;
    } else {
        header->req_stream.read_end = 1;
    }
}

void _clear_req_head(req_attr_t *attr) {
    if(attr->Method)
         dts_free_null(&attr->Method);
    if(attr->path)
         dts_free_null(&attr->path);
    if(attr->Cache_control)
         dts_free_null(&attr->Cache_control);
    if(attr->User_Agent)
         dts_free_null(&attr->User_Agent);
    if(attr->Accept)
         dts_free_null(&attr->Accept);
    if(attr->Accept_Language)
         dts_free_null(&attr->Accept_Language);
    if(attr->Accept_encoding)
         dts_free_null(&attr->Accept_encoding);
    if(attr->Host)
         dts_free_null(&attr->Host);
    if(attr->Connection)
         dts_free_null(&attr->Connection);
    if(attr->Content_Type)
         dts_free_null(&attr->Content_Type);
    if(attr->Origin)
         dts_free_null(&attr->Origin);
    if(attr->Referer)
         dts_free_null(&attr->Referer);
}
